package xin.yangshuai.juc01.hello;

import java.util.ArrayList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Question1
 *
 * @author shuai
 * @date 2022/2/10
 */

class Question11 {
    Lock lock = new ReentrantLock();
    private int index = 0;

    Condition condition = lock.newCondition();

    public void printA(int num, int len) throws InterruptedException {
        lock.lock();
        try {
            while (index != 0) {
                condition.await();
            }
            for (int i = 0; i < num; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + len + ":" + i + ":A");
            }
            condition.signalAll();
            index = 1;
        } finally {
            lock.unlock();
        }
    }

    public void printB(int num, int len) throws InterruptedException {
        lock.lock();
        try {
            while (index != 1) {
                condition.await();
            }
            for (int i = 0; i < num; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + len + ":" + i + ":B");
            }
            condition.signalAll();
            index = 2;
        } finally {
            lock.unlock();
        }
    }

    public void printC(int num, int len) throws InterruptedException {
        lock.lock();
        try {
            while (index != 2) {
                condition.await();
            }
            for (int i = 0; i < num; i++) {
                System.out.println(Thread.currentThread().getName() + ":" + len + ":" + i + ":C");
            }
            condition.signalAll();
            index = 0;
        } finally {
            lock.unlock();
        }
    }
}

public class Question1 {

    public static void main(String[] args) {
        Question11 question11 = new Question11();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        question11.printA(5, i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "AA").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        question11.printB(10, i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "BB").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        question11.printC(15, i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "CC").start();

        ArrayList list = new ArrayList();
        list.toString();

    }
}
